package com.haiyi.software.xmpp.service;

import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import at.theengine.android.bestlocation.BestLocationListener;
import at.theengine.android.bestlocation.BestLocationProvider;
import at.theengine.android.bestlocation.BestLocationProvider.LocationType;

public abstract class HIChatServicesLocation extends
		HIChatServicesXMPPOperation {

	private BestLocationProvider mBestLocationProvider;
	private BestLocationListener mBestLocationListener;
	private static String TAG = "HIChatServicesLocation";

	@Override
	public void onCreate() {
		// TODO 自动生成的方法存根
		super.onCreate();
		initLocation();
		mBestLocationProvider
				.startLocationUpdatesWithListener(mBestLocationListener);
	}

	@Override
	public void onDestroy() {
		// TODO 自动生成的方法存根
		super.onDestroy();
		initLocation();
		mBestLocationProvider.stopLocationUpdates();
	}

	private void initLocation() {
		if (mBestLocationListener == null) {
			mBestLocationListener = new BestLocationListener() {

				@Override
				public void onStatusChanged(String provider, int status,
						Bundle extras) {
					Log.i(TAG, "onStatusChanged PROVIDER:" + provider
							+ " STATUS:" + String.valueOf(status));
				}

				@Override
				public void onProviderEnabled(String provider) {
					Log.i(TAG, "onProviderEnabled PROVIDER:" + provider);
				}

				@Override
				public void onProviderDisabled(String provider) {
					Log.i(TAG, "onProviderDisabled PROVIDER:" + provider);
				}

				@Override
				public void onLocationUpdateTimeoutExceeded(LocationType type) {
					Log.w(TAG, "onLocationUpdateTimeoutExceeded PROVIDER:"
							+ type);
				}

				@Override
				public void onLocationUpdate(Location location,
						LocationType type, boolean isFresh) {
					Log.i(TAG, "onLocationUpdate TYPE:" + type + " Location:"
							+ mBestLocationProvider.locationToString(location));
					// mTvLog.setText("\n\n" + new Date().toLocaleString()
					// + "\nLOCATION UPDATE: isFresh:"
					// + String.valueOf(isFresh) + "\n"
					// + mBestLocationProvider.locationToString(location)
					// + mTvLog.getText());
				}
			};

			if (mBestLocationProvider == null) {
				mBestLocationProvider = new BestLocationProvider(this, true,
						true, 10000, 1000, 2, 0);
			}
		}
	}
}
